package com.cuit.springboot.util;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;

//保证序列化json的时候，如果是null的对象，key也会消失
public class ServerResponse<T> implements Serializable{
    /**
     * serialVersionUIDadd by wanglei at 2018-1-29 上午11:35:41
     * 
     */
    private static final long serialVersionUID = 1L;
    private int status;
    private String info;
    private T data=null;

    private ServerResponse(int status){
        this.status=status;
    }
    private ServerResponse(int status,T data){
        this.status=status;
        this.data=data;
    }
    private ServerResponse(int status,String info,T data){
        this.status=status;
        this.info=info;
        this.data=data;
    }
    private ServerResponse(int status,String info){
        this.status=status;
        this.info=info;
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public  boolean isSuccess(){
        return  this.status==ResponseCode.SUCCESS.getStatus();
    }

    public int getStatus(){
        return status;
    }
    public T getData(){
        return data;
    }
    public String getInfo(){
        return info;
    }

    public static  ServerResponse createBySuccess(){
        return  new ServerResponse(ResponseCode.SUCCESS.getStatus(),ResponseCode.SUCCESS.getInfo());
    }

    public static  ServerResponse createBySuccess(String info){
        return  new ServerResponse(ResponseCode.SUCCESS.getStatus(),info);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(),data);
    }

    public static <T> ServerResponse<T> createBySuccess(String info,T data){
        return  new ServerResponse<T>(ResponseCode.SUCCESS.getStatus(),info,data);
    }

    public static  ServerResponse createByError(){
        return new ServerResponse(ResponseCode.ERROR.getStatus(),ResponseCode.ERROR.getInfo());
    }

    public static  ServerResponse createByError(String errorMessage){
        return new ServerResponse(ResponseCode.ERROR.getStatus(),errorMessage);
    }

    public static  ServerResponse createByError(int errorCode,String errorMessage){
        return new ServerResponse(errorCode,errorMessage);
    }
    /**
     * add by xuji at 2018-4-27 下午4:33:52
     * @describe: 未认证
     * @return
     */
    public static  ServerResponse createByUNAUTHC(){
        return new ServerResponse(ResponseCode.NEED_LOGIN.getStatus(),ResponseCode.NEED_LOGIN.getInfo());
    }
    /**
     * add by xuji at 2018-4-27 下午4:33:56
     * @describe: 参数错误
     * @return
     */
    public static  ServerResponse createByILLEGAL_ARGUMENT(){
        return new ServerResponse(ResponseCode.ILLEGAL_ARGUMENT.getStatus(),ResponseCode.ILLEGAL_ARGUMENT.getInfo());
    }
    
    public static  ServerResponse createByRuntimeError(String errorMessage){
    	//这里可以加入异常提醒代码
        return new ServerResponse(ResponseCode.RUNTIME_ERROR.getStatus(),DateUtil.getDate(new Date(), "yyyy-MM-dd HH:mm:ss")+" "+ResponseCode.RUNTIME_ERROR.getInfo()+":"+errorMessage);
    }
}
